/**
 * 
 */
package edu.sjtu.seda.experiment;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import edu.sjtu.seda.event.Event;
import edu.sjtu.seda.event.Event.STATUS;
import edu.sjtu.seda.main.StagePlatform;
import edu.sjtu.seda.test.Message;

/**
 * @author john.zhu
 *
 */
public class SedaTest {
	
	public void init(){
	}
	
	static class TestThread1 implements Runnable {

		private int i = 0;
		
        private StagePlatform stages;
        
        private List<Event> events;
		
		public TestThread1(int i, StagePlatform stages,List<Event> events){
			this.i = i;
			this.stages = stages;
			this.events = events;
		}
		
		@Override
		public void run() {
			Message m = new Message("Message "+i);
			m.pushLocation(stages.getStage("stage2"));
			m.pushLocation(stages.getStage("stage1"));
			events.add(m);
			stages.pushEvent(m);
		}
	}
	
	static class TestThread2 implements Runnable {

		private int i = 0;
		
        private StagePlatform stages;
        
        private List<Event> events;
		
		public TestThread2(int i, StagePlatform stages,List<Event> events){
			this.i = i;
			this.stages = stages;
			this.events = events;
		}
		
		@Override
		public void run() {
			Message m = new Message("Message "+i);
			m.pushLocation(stages.getStage("stage3"));
			m.pushLocation(stages.getStage("stage1"));
			events.add(m);
			stages.pushEvent(m);
		}
	}
	
	
	public static void main(String[] args) {
		long startNano = System.nanoTime();
		File file = new File("d://b.txt");
		FileWriter write = null;
		try {
			write = new FileWriter(file);
			write.write(new SimpleDateFormat("MM/dd/yy HH:mm:ss.SSS").format(new Date()));
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				write.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		List<Event> events = new ArrayList<Event>(10000);
		StagePlatform stages = new StagePlatform();
		stages.start();
		
		for (int i = 0; i < 1000; i++) {
			if(i%2==0){
				Thread t1 = new Thread(new TestThread1(i, stages,events));
				t1.start();
			}
			else{
				Thread t2 = new Thread(new TestThread2(i, stages,events));
				t2.start();
			}
		}
		
//		try {
//			TimeUnit.MILLISECONDS.sleep(10000);
//		} catch (InterruptedException e1) {
//			e1.printStackTrace();
//		}
//		
//		stages.destroy();
//		int start=0;
//		int reject=0;
//		int finish=0;
//		for(Event e :events){
//			if(e.getStatus().equals(STATUS.START)){
//				start++;
//				System.out.println("event "+e.getUID()+" status start");
//			}
//			if(e.getStatus().equals(STATUS.REJECTED))
//				reject++;
//			if(e.getStatus().equals(STATUS.FINISH))
//				finish++;
//		}
//		System.out.println("START: "+start+" REJECTED: "+reject+" FINISH: "+finish);
//		System.out.println("Spent time:"+(System.nanoTime()-startNano)+" nano seconds");
	}

}
